﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
using DTN.Core.Common;
using DTN.Core.Utilities;
namespace com.dtn.deals
{
    public class tbl_FollowManager : Itbl_FollowManager
    {
        private static Itbl_FollowManager itbl_FollowtManager = null;
        private tbl_FollowManager()
        {
        }
        public static Itbl_FollowManager CreateInstance()
        {
            if (itbl_FollowtManager == null)
            {
                itbl_FollowtManager = new tbl_FollowManager();
            }
            return itbl_FollowtManager;
        }
        public int Inserttbl_Follow(tbl_Follow tbl_Follow)
        {
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
                                                                new SqlParameter("@userId", SqlDbType.Int),
																new SqlParameter("@partnerId", SqlDbType.Int),																
																new SqlParameter("@cDate", SqlDbType.DateTime),																
																new SqlParameter("@isDel", SqlDbType.TinyInt),
																
															};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = tbl_Follow.userID;
            parameters[2].Value = tbl_Follow.partnerID;
            parameters[3].Value = tbl_Follow.cDate;
            parameters[4].Value = tbl_Follow.isDel;
           
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_Follow", CommandType.StoredProcedure, parameters);
                tbl_Follow.ID = Convert.ToInt32(parameters[0].Value);
            }
            catch (Exception ex)
            {
                tbl_Follow.ID = -1;
            }
            return tbl_Follow.ID;
        }
        //thêm khung giờ và saleoff tương ứng với sản phẩm khi insert
        

        public bool Updatetbl_Follow(tbl_Follow  tbl_Follow)
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
                                                                new SqlParameter("@userId", SqlDbType.Int),
																new SqlParameter("@partnerId", SqlDbType.Int),																new SqlParameter("@proName", SqlDbType.NVarChar),
																new SqlParameter("@cDate", SqlDbType.DateTime),																
																new SqlParameter("@isDel", SqlDbType.TinyInt),
															};
            parameters[0].Value = tbl_Follow.ID;       
            parameters[1].Value = tbl_Follow.userID;
            parameters[2].Value = tbl_Follow.partnerID;
            parameters[3].Value = tbl_Follow.cDate;
            parameters[4].Value = tbl_Follow.isDel;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_Follow", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }

        
        public bool Deletetbl_Follow(int id)
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
            parameters[0].Value = id;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_Follow", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }

        
        public tbl_Follow Selecttbl_FollowtByID(int id)
        {
            IDataReader dr;
            tbl_Follow  tbl_Follow = new tbl_Follow();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
            parameters[0].Value = id;
            try
            {
                dr = dataProvider.ExecuteReader("sp_Selecttbl_Follow", CommandType.StoredProcedure, parameters);
                while (dr.Read())
                {
                    tbl_Follow.ID = Convert.ToInt32(dr["ID"].ToString());
                    tbl_Follow.userID = Convert.ToInt32(dr["userID"].ToString());
                    tbl_Follow.partnerID  = Convert.ToInt32(dr["partnerID"].ToString());
                    tbl_Follow .isDel = Convert.ToInt32(dr["isDel"].ToString());
                    tbl_Follow .cDate = Convert.ToDateTime(dr["createDate"].ToString());
                   
                }

            }
            catch (Exception ex)
            {
                return null;
            }
            return tbl_Follow;
        }
        public DataTable Selecttbl_FollowDynamic(string Where, string Order)
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
            parameters[0].Value = Where;
            parameters[1].Value = Order;
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_FollowDynamic", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }
     
    }
}
